package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.system.AppVersionEntity;
import com.dragon.modules.yuegou.enums.code.SystemCode;
import com.dragon.modules.yuegou.enums.system.AppVersionPrdType;
import com.dragon.modules.yuegou.enums.system.AppVersionType;
import com.dragon.modules.yuegou.mapper.system.AppVersionMapper;
import com.dragon.modules.yuegou.model.system.AppVersion;
import com.dragon.modules.yuegou.query.system.AppVersionQuery;
import com.dragon.modules.yuegou.service.system.AppVersionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * app版本管理 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-24 17:59:35
 * @version 2.0
 */
@Service("sysAppVersionService")
public class AppVersionServiceImpl
		extends ServiceImpl<AppVersionMapper, AppVersionEntity>
		implements AppVersionService {

	
	@Override
	public IPage<AppVersion> search(PageRequest page, AppVersionQuery query) {
		IPage<AppVersionEntity> pageEntity = new Page<>(page.getPage(), page.getSize(), page.getCount());
		QueryWrapper<AppVersionEntity> qw = query.toQueryWrapper();
		qw.orderByDesc(AppVersion.COL_CREATED_TIME);

		IPage<AppVersionEntity> pageEntites = baseMapper.selectPage(pageEntity, qw);
		return pageEntites.convert(AppVersion::new);
	}

	@Transactional
	@Override
	public void saveAppVersion(AppVersion appVersion) {
		if (StringUtils.isEmpty(appVersion.getId())) {
			if (appVersion.getLastVersion()) {
				nrwest(appVersion);
			}
			//APP插入信息
			baseMapper.insert(appVersion);
		}else {
			AppVersionEntity version = baseMapper.selectById(appVersion.getId());
			if (version.getLastVersion()) {
				nrwest(appVersion);
				appVersion.setLastVersion(version.getLastVersion());
			}
			//APP修改信息
			baseMapper.updateById(appVersion);
		}
	}

	@Override
	public void deleteAppVersion(String appId) {
		AppVersionEntity appVersion = baseMapper.selectById(appId);
		if (appVersion.getLastVersion()) {
			throw new DragonException(SystemCode.LAST_VERSION_NOT_DELETE);
		}
		removeById(appId);
	}

	@Override
	public void latestVersion(String appId) {
		//获取当前APP类型
		AppVersionEntity appVersion = baseMapper.selectById(appId);
		nrwest(appVersion);
		//设置当前APP为最新版本APP
		appVersion.setLastVersion(true);
		baseMapper.updateById(appVersion);
	}
	
	private void nrwest(AppVersionEntity appVersion) {
		AppVersionQuery appVersionQuery = new AppVersionQuery();
		appVersionQuery.setType(appVersion.getType());
		appVersionQuery.setAppId(appVersion.getAppId());
		appVersionQuery.setPrdType(appVersion.getPrdType());
		appVersionQuery.setLastVersion(true);
		// 获取该类型最新APP信息，设置为不是最新版本
		AppVersion version = getOne(appVersionQuery);
		if (version != null) {
			version.setLastVersion(false);
			baseMapper.updateById(version);
		}
	}

	@Override
	public AppVersion latestVersion(String appId, AppVersionType type, AppVersionPrdType prdType) {
		AppVersionQuery query = new AppVersionQuery();
		query.setType(type);
		query.setAppId(appId);
		query.setPrdType(prdType);
		query.setLastVersion(true);
		return getOne(query);
	}

	private AppVersion getOne(AppVersionQuery appVersionQuery){
		List<AppVersionEntity> app = baseMapper.selectList(appVersionQuery.toQueryWrapper());
		if (CollectionUtils.isEmpty(app)){
			return null;
		}
		return new AppVersion(app.get(0));
	}

}